# -*- coding: utf-8 -*-

from odoo import models, fields, api
import datetime
class HrAttendanceReport(models.Model):
    _name = 'hr.attendance.report'
    _description = '考勤日报'
    employee_id=fields.Many2one('hr.employee')
    date=fields.Date(string=u'日期')
    department_id = fields.Many2one('hr.department', compute='compute_department_id', store=True)
    month_time=fields.Char(compute='compute_month_time',string='月份',store=True)
    @api.depends('date')
    def compute_month_time(self):
        for record in self:
            record.month_time=record.date.strftime('%Y-%m-%d')[0:7]

    @api.depends('employee_id')
    def compute_department_id(self):
        for r in self:
            r.department_id = r.employee_id.department_id

    resource_calendar_id = fields.Many2one('resource.calendar', compute='compute_resource_calendar_id', store=True)

    @api.depends('date', 'employee_id')
    def compute_resource_calendar_id(self):
        for record in self:
            record.resource_calendar_id = record.employee_id.get_current_calendar_id(record.date)

    check_in=fields.Datetime(compute='compute_check_time',string='实际刷卡上班')
    check_out = fields.Datetime(compute='compute_check_time',string='实际刷卡下班')
    @api.depends('attendance_id')
    def compute_check_time(self):
        for r in self:
            r.check_in=r.attendance_id.check_in
            r.check_out=r.attendance_id.check_out
    attendance_id=fields.Many2one('hr.attendance')
    attendance_type=fields.Selection([
        ('1','正常'),
        ('0', '异常'),
    ],string='状态')
    is_late=fields.Float(string='迟到(h)')
    is_absent=fields.Integer(string='旷工(天数)')
    is_leave_early=fields.Float(string='早退(h)')
    overtime=fields.Float(string=u'加班(h)')

    def convert_to_date_time(self,val):
        if not isinstance(val,str):
            val.strftime('%Y-%m-%d %H:%M:%S')
        date, time = val.split(' ')
        hour, min, s = str(time).split(':')
        min=float(min)/60
        return float(hour)+float(min)


    @api.depends('attendance_id.checkin','attendance_id.checkout')
    def compute_report_type(self):
        for r in self:

            r.is_absent=0
            r.is_late=0
            r.is_leave_early=0
            r.overtime=0
            r.attendance_type='1'
            attendances=r.employee_id.get_employee_attendance(r.date)

            # 没有考勤pass
            if r.attendance_id and not r.employee_id.get_current_calendar_time(r.date):
                r.is_absent=0
                r.is_late=0
                r.is_leave_early=0
            elif r.attendance_id and (not r.attendance_id.check_in  or not r.attendance_id.check_out) and attendances:
                r.is_absent = 1
                r.attendance_type = '0'
            elif r.attendance_id and r.attendance_id.check_in and r.employee_id.get_current_calendar_time(r.date) and attendances:
                calendar_id=r.employee_id.get_current_calendar_id(r.date)
                time_in,time_out=r.employee_id.get_current_calendar_time(r.date)



                if float(time_out)<12:
                    time_out=float(time_out)+24

                check_in=(r.attendance_id.check_in+datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
                check_in=self.convert_to_date_time(check_in)
                check_out = (r.attendance_id.check_out + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
                lunch_hour_to=calendar_id.lunch_hour_to
                check_out = self.convert_to_date_time(check_out)
                print (lunch_hour_to)
                if r.resource_calendar_id.calendar_type=='3':
                    if check_in<12:
                        check_in+=24
                    if float(check_out) < 12 and r.employee_id:
                        check_out = float(check_out) + 24
                if float(check_in)> float(time_in):
                    r.is_late=round(float(check_in)-float(time_in),2)
                    lunch_hour = calendar_id.lunch_time
                    if float(check_in)>float(lunch_hour_to):
                        r.is_late=round(float(check_in)-float(time_in)-float(lunch_hour),2)
                else:
                    r.is_late=False
                if float(check_out)<float(time_out) and r.date!=fields.date.today():
                    r.is_leave_early=round(float(time_out)-float(check_out),2)
                else:
                    r.is_leave_early=False
                # if float(check_in)<= float(time_in) and float(check_out)>=float(time_out):
                #     r.attendance_type='1'
                # else:
                #     r.attendance_type='0'
                r.is_absent=False
            elif not r.attendance_id and attendances :
                r.is_absent=1
            elif attendances and not r.attendance_id:
                r.is_absent=0
            overtime = self.env['hr.overtime'].search(
                [('employee_id', '=', r.employee_id.id), ('apply_date', '=', r.date.strftime('%Y-%m-%d'))])
            if overtime:
                r.overtime = overtime.real_period
            if r.is_late or r.is_absent or r.is_leave_early:
                r.attendance_type='0'
            else:
                r.attendance_type='1'
            # 无需考勤的人
            if r.employee_id.attendance_free:
                r.is_absent = 0
                r.is_late = 0
                r.is_leave_early = 0
                r.overtime = 0
                r.attendance_type = '1'


